//
// Created by wujehy on 2020/8/3.
//

#include "Client.h"
#include "MyCodecType.h"
#include <handy/handy.h>
#include "Network.pb.h"
#include "GeeJoan/NetWorkPackage.pb.h"
#include "GeeJoan/AuthModule.pb.h"

using namespace handy;

void Client::init()
{

    info( " test init s = %s , p = %d ", host.c_str(), port );
    m_client = TcpConn::createConnection( this->multiBase->allocBase(), host.c_str(), port, 3000 );
    std::cout << " start client " << std::endl;
    initLister();
}

void Client::deinit()
{

}

void Client::initLister()
{

    // 客户端接受
    m_client->onMsg(
            new MyCodecType,
            [&](const TcpConnPtr &con, Slice msg)
            {
                info( "recv msg: %d %.*s", (int) msg.size(), msg.data());

                std::string command( msg.data(), msg.size());
                for (int i = 0; i < command.length(); ++i)
                {
                    printf( "%02x ", (int) (command.at( i )));
                }
                if (command == "hello0000")
                {
                    this->startTask();
                }
//                if (command.substr(0, 6) == "reload") {
//                    this->relink_Client();
//                    info("reload m client");
//
//                } else if (command.substr(0, 5) == "close") {
//                    this->close_Client();
//                    info("close m client");
//
//                } else if (command.substr(0, 5) == "setip") {
//                    std::string testip = command.substr(5, command.length() - 1);
//                    info("data ip : %s ", testip.c_str());
//                    this->current_ip = std::string(testip.c_str(), testip.length());
//
//                } else if (command.substr(0, 7) == "setport") {
//                    std::string testip = command.substr(7, command.length() - 1);
//                    info("data port : %s ", testip.c_str());
//                    this->current_port = std::atoi(testip.c_str());
//                } else if (command.substr(0, 4) == "tran") {
//                    std::string messageData = command.substr(4, command.length() - 1);
//                    con->send(messageData);
//                } else if (command.substr(0, 4) == "init") {
//                    this->init_Client();
//                    info("init m client");
//                } else if (command.substr(0, 4) == "stat") {
//                    info("sercer : %s , p = %d ", current_ip.c_str(), current_port);
//                }
            } );
    m_client->onState( [=](const TcpConnPtr &con)
                       {

                           using GeeJoan::Protocol::NetWork::NetWorkPackageRequest;
                           using GeeJoan::Protocol::NetWork::NetWorkPackageModel;
                           info( "onState called state: %d", con->getState());
                           if (con->getState() == TcpConn::Connected)
                           {
                               // 发送一个校验信息
                               std::cout << " send " << std::endl;
                               GetMasterRequest getMasterRequest;
                               getMasterRequest.set_key( "test" );
                               NetWorkPackageRequest netWorkPacket;
                               netWorkPacket.set_type_m( NetWorkPackageModel( 1 ));
                               netWorkPacket.set_type_sub( 1 );
                               std::string data;
                               getMasterRequest.SerializeToString( &data );
                               netWorkPacket.set_body( data );
                               printf( "\n" );
                               for (int i = 0; i < data.length(); ++i)
                               {
                                   printf( "%02x ", (int) (data.at( i )));
                               }
                               printf( "\n" );
                               con->sendMsg( netWorkPacket.SerializeAsString());
                               info( "send Success " );
                           } else if (con->getState() == TcpConn::Failed)
                           {
                               // 链接失败 重连其他服务器

                           }
                       } );
}

void Client::startTask()
{
    // 自动任务
    std::cout << " startTask " << std::endl;
    multiBase->allocBase()->runAfter( 3000, []()
    {
        std::cout << " hello  " << std::endl;
    }, 3000 );
}
